local CommonOHOH = _G.CommonOHOH;
local ClientOHOH = _G.ClientOHOH;

local M = ClientOHOH.class.new(script, ClientOHOH.files.Client_PlayerComponent())

local Tool=CommonOHOH.load.ShootTool()
local EventList = ClientOHOH.load.EventList();


function M.new(self,...)
	return M(self,...);
end;

function M.ctor(self, parent)
	M.super.ctor(self, parent);
end;

--寻找射击对象
function M.FindShoot(self)

    local list=self.FindAttackTargetEvent:Action(self.character,self.parent)
    for _,v in pairs(list) do
        return v  
    end

   local cf= self:GetBulletStartCframe()
   local direction=cf.LookVector.Unit* self.Lenght 
   return {pos=cf.Position+direction}
end


-- 获取子弹起始位置
function M.GetBulletStartCframe(self)
    return   self.character:GetCFrame()
end


function M.GetSendStartPos(self,userId)
   local player= game.Players:GetPlayerByUserId(userId)
   if player.Character then
        return player.Character:GetPrimaryPartCFrame().Position
   end
end

-- 发射子弹
function M.SendBullet(self,userId,bulletId,bulletSpeed,endPos,startPos)
    
  if not startPos then
         startPos=self:GetSendStartPos(userId)
        if not startPos then
            return 
        end
  end

  local bullet= _G.POOL:GetObj(bulletId)
  local sendInfo=  Tool.Shoot(startPos, endPos,bullet,bulletSpeed)

  --监听子弹被销毁事件
  sendInfo.dtorEvent:Connect(function()
        _G.POOL:PushObj(bulletId,bullet)
  end)
  
end

function M.dtor(self)
	M.super.dtor(self);
end;

M:RegistEvent(EventList.Other_Player_Shoot,M.SendBullet)

return M